//
//  GifController.m
//  UIImagePickController
//
//  Created by DYS on 15/12/11.
//  Copyright © 2015年 DYS. All rights reserved.
//

#import "GifController.h"
#import "NSGIF.h"

@interface GifController ()<UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet UIButton *btn;

@property(nonatomic)UIImagePickerController *pick;
@end

@implementation GifController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (UIImagePickerController *)pick{
    if (_pick == nil) {
        _pick = [[UIImagePickerController alloc] init];
        _pick.delegate = (id)self;
    }
    return _pick;
}

- (IBAction)btnClick:(id)sender {
    //判断是否为模拟器
//#ifdef TARGET_IPHONE_SIMULATOR
//    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"无法打开摄像头" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
//    [alertView show];
//    return;
//#endif
    
    
    self.pick.delegate = (id)self;
    self.pick.sourceType = UIImagePickerControllerSourceTypeCamera;
    self.pick.videoQuality = UIImagePickerControllerQualityTypeMedium;
    //设置拍照还是摄像
    self.pick.mediaTypes = @[(NSString *)kUTTypeMovie];
    [self presentViewController:self.pick animated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self.pick dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    [self.pick dismissViewControllerAnimated:YES completion:nil];
    NSURL *url = info[@"UIImagePickerControllerMediaURL"];
    if (url) {
        self.btn.alpha = 0.0;
        [NSGIF optimalGIFfromURL:url loopCount:0 completion:^(NSURL *GifURL) {
            [self.webView loadRequest:[NSURLRequest requestWithURL:GifURL]];
        }];
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
